//
//  WHYHTTPManager.m
//  News163
//
//  Created by 王洪运 on 16/1/1.
//  Copyright © 2016年 王洪运. All rights reserved.
//

#import "WHYHTTPManager.h"

typedef NS_ENUM(NSInteger, WHYHTTPMethod) {
    kWHYHTTPMethodGet = 0,
    kWHYHTTPMethodPOST = 1
};

@implementation WHYHTTPManager

#pragma mark - 单例方法
+(instancetype)manager {
//    UIButtonType
    WHYHTTPManager *manager = [super manager];
    
    NSMutableSet *set = [NSMutableSet set];
    set.set = manager.responseSerializer.acceptableContentTypes;
    [set addObject:@"text/html"];
    
    manager.responseSerializer.acceptableContentTypes = set;
    
    return manager;
}

#pragma mark - 加载新闻详情数据
-(void)loadNewsDetailRequestWithDocid:(NSString *)docid finishedHandler:(WHYHTTPFinishedHandler)finished {
    NSString *urlString = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html",docid];
    
    [self requestWithMethod:kWHYHTTPMethodGet urlString:urlString parameters:nil finishedHandler:finished];
}

#pragma mark - 加载新闻数据
-(void)loadNewsRequestWithNewsId:(NSString *)newsId finishedHandler:(WHYHTTPFinishedHandler)finished {
    NSString *urlStr = [kNetEaseDomain stringByAppendingString:newsId];
    urlStr = [NSString stringWithFormat:@"%@/0-140.html",urlStr];
    
    [self requestWithMethod:kWHYHTTPMethodGet urlString:urlStr parameters:nil finishedHandler:finished];
}

#pragma mark - 发送request请求
-(void)requestWithMethod:(WHYHTTPMethod)method urlString:(NSString *)urlString parameters:(id)parameters finishedHandler:(WHYHTTPFinishedHandler)finishedHandler {
    
    void (^success)(AFHTTPRequestOperation *operation, id responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject) {
        finishedHandler(responseObject, nil);
    };
    
    void (^failure)(AFHTTPRequestOperation *operation, NSError *error) = ^(AFHTTPRequestOperation *operation, NSError *error) {
        finishedHandler(nil, error);
    };
    
    if (method == kWHYHTTPMethodGet) {
        [[WHYHTTPManager manager] GET:urlString parameters:parameters success:success failure:failure];
    }else if (method == kWHYHTTPMethodPOST) {
        [[WHYHTTPManager manager] POST:urlString parameters:parameters success:success failure:failure];
    }
    
}



@end







